package com.app.miwawa.api.system.service.bo;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.framework.common.api.entity.CommonEntityAware;
import org.framework.common.api.service.bo.CommonBoAdapter;
import org.framework.common.api.service.dao.CommonDaoAware;
import org.framework.common.api.support.model.PageModel;
import org.framework.common.api.support.model.QueryModel;
import org.framework.common.api.websocket.handler.WebsocketEndSynchs;
import org.framework.common.ext.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.app.miwawa.api.system.entity.Easemob;
import com.app.miwawa.api.system.entity.view.NEasemobView;
import com.easemob.server.example.utils.EasemobUtils;

@Service("easemobBo")
public class EasemobBo extends CommonBoAdapter<Easemob, String> {
	
	@Resource(name="defaultDao")
	private CommonDaoAware<CommonEntityAware<String>, String> dao;
	
	@Autowired
	private JdbcTemplate jdbc;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Object register(final String key, final String mobile) throws Exception{
		String uid = "";
		String eid = "";
		String type = "";
		String shopsid = "";
		String password = "";
		
		NEasemobView user = (NEasemobView)dao.loader((Class)NEasemobView.class, "mobile", mobile);
		
		if(user == null){
			return "未注册环信的帐户中，未发现指定帐户";
		}else{
			uid = user.getId();
			eid = user.getEid();
			type = user.getType();
			shopsid = user.getShopsid();
			password = user.getPassword();
			
			if(!EasemobUtils.getIMUser(eid) && EasemobUtils.createIMUser(eid, password, mobile)){
				dao.insert(new Easemob(uid, eid, type, mobile, shopsid));
				return "环信帐户注册成功！";
			}else{
				return "指定帐户已在环信注册，或注册失败！";
			}
		}
	}

	/**
	 * 注销环信帐户.
	 * @param id
	 * @return
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年5月17日 下午5:10:04
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public boolean delete(final String id) throws Exception {
		// 持久化实体对象
		Easemob easemob = (Easemob)dao.loader((Class)Easemob.class, id);
		
		// 注销环信帐户
		if(EasemobUtils.deleteIMUser(easemob.getEid())){
			dao.remove(easemob);
		}
		
		return true;
	}
	
	/**
	 * 同步环信账号.
	 * @return
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年5月28日 下午9:25:49
	 */
	@SuppressWarnings("unchecked")
	public void sync(final WebsocketEndSynchs point) throws Exception{
		point.sendMessage("正在检测环信账号...");
		QueryModel query = new QueryModel(NEasemobView.class, 0, 0);
		PageModel<CommonEntityAware<String>> page = dao.findByQuery(query);
		List<NEasemobView> result = (List<NEasemobView>)page.getRows();
		if(result != null && !result.isEmpty()){
			for (NEasemobView easemob : result) {
				String uid = easemob.getId();
				String eid = easemob.getEid();
				String type = easemob.getType();
				String mobile = easemob.getMobile();
				String shops = easemob.getShops();
				String shopsid = easemob.getShopsid();
				String password = easemob.getPassword();
				
				StringBuilder message = new StringBuilder(shops).append(":").append(type).append(":").append(mobile);
				if(StringUtil.isNotEmpty(mobile) && !EasemobUtils.getIMUser(eid) && EasemobUtils.createIMUser(eid, password, mobile)){
					dao.insert(new Easemob(uid, eid, type, mobile, shopsid));
					point.sendMessage(message.append("同步成功").toString());
				}else{
					point.sendMessage(message.append("同步失败").toString());
				}
			}
		}
		point.sendMessage("环信帐户同步结束");
	}

	/**
	 * 刷新环信昵称.
	 * @author chanlong(陈龙)
	 * @date 2016年6月15日 上午11:22:16
	 */
	public synchronized void nick() {
		List<Map<String,Object>> result = jdbc.queryForList("call pro_appparents();");
		for (Map<String, Object> map : result) {
			
			try {
				String id = String.valueOf(map.get("id"));
				String nick = String.valueOf(map.get("nick"));
				System.out.println("C"+id+","+nick);
				EasemobUtils.modifyNickname("C"+id, nick);
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
